package com.example.mongodb.mongodb.controller.rabbitmq.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 工作队列模式 workQueue
 */
public class NewTask {

    private static String QUEUE_NAME ="";
    private static String[] works={
            "hello.",
            "hello..",
            "hello...",
            "hello....",
            "hello.....",
            "hello......",
            "hello.......",
            "hello........",
            "hello.........",
            "hello..........",
            "hello...........",
            "hello............",
            "hello.............",
            "hello..............",
            "hello...............",
            "hello................",
            "hello.................",
            "hello..................",
            "hello...................",
            "hello....................",
            "hello.....................",
            "hello......................",
            "hello.......................",
            "hello........................",
    };


    public static void main(String[] args) {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("123.207.179.78");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");

        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();


            channel.queueDeclare("taskQueue",false,false,false,null);

            for (String work : works) {
                //将消息路由到taskQueue队列中

                channel.basicPublish("","taskQueue",null,work.getBytes());
                System.out.println("[x] Sent "+work);
            }


        }  catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
